package com.summer.ChengXuYuanMianShiJingDian;

public class Q1_5 {
    public boolean oneEditAway(String first, String second) {
        if (first.length()>second.length()){
            int index =-1;
            if (first.length()==second.length()+1){
                for (int i = 0;i<second.length();i++){
                    if (first.charAt(i)!=second.charAt(i)){
                        index =i;
                        break;
                    }
                }
                StringBuilder s = new StringBuilder(first);
                if (index!=-1){
                    s.deleteCharAt(index);
                }else s.deleteCharAt(first.length()-1);
                if (s.toString().equals(second))return true;
                else return false;

            }else return false;
        }
        if (first.length()==second.length()){
            if (first.equals(second))return true;
            int index =-1;
            for (int i = 0;i<second.length();i++){
                if (first.charAt(i)!=second.charAt(i)){
                    index =i;
                    break;
                }
            }
            StringBuilder s = new StringBuilder(first);
            if (index!=-1){
                s.replace(index,index+1, String.valueOf(second.charAt(index)));
            }else return false;

            if (s.toString().equals(second)) return true;
            else return false;
        }
        if (first.length()<second.length()){
            return oneEditAway(second,first);
        }
        return false;
    }
}
